package com.patrick.open.api.core.exception;

import com.patrick.open.api.core.common.RespData;
import com.patrick.open.api.core.utils.JsonGzp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义访问拒绝
 */
@Slf4j
public class OpenAccessDeniedHandler implements AccessDeniedHandler {

  @Override
  public void handle(HttpServletRequest request, HttpServletResponse response,
                     AccessDeniedException exception) throws IOException, ServletException {
    RespData resultBody = OpenGlobalExceptionHandler.resolveException(exception, request.getRequestURI());
    response.setStatus(resultBody.getHttpStatus());
    JsonGzp.writeJson(response, resultBody);
  }
}
